/**
 * @Author MrsGuo
 * version 1.0
 * @Date 2023-08-07 15:48
 */
package com.cl.erp_2301.controller;

import com.cl.erp_2301.Service.*;

import com.cl.erp_2301.dto.*;
import com.cl.erp_2301.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jdk.swing.interop.SwingInterOpUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v2/orders")
public class MarketController {
    @Resource
    private MarketRequestService marketRequestService;
    @Resource
    private MarketAddService marketAddService;
    @Resource
    private SupplierService supplierService;
    @Resource
    private SourcingGoodsService sourcingGoodsService;
    @Resource
    private CheckMarketService checkService;
    @Resource
    private StartService startService;

    @GetMapping("/listByPage")
    public Result findMarketRequest(Integer state, int page, int rows) {
        Page<Object> marketPage = PageHelper.startPage(page, rows);
        DataDTO data = new DataDTO();
        //不传state
        if (state == null) {
            List<MarketDTO> orderInquiryDTOS = marketRequestService.selectOrderInquiry();
            for (MarketDTO orderDetailDTO : orderInquiryDTOS) {
                List<MarketDTO> marketDetailDTOList = marketRequestService.selectMarketDetailByUuid(orderDetailDTO.getUuid());
                orderDetailDTO.setOrderDetailList(marketDetailDTOList);
            }
            data.setRows(orderInquiryDTOS);
        } else {
            List<MarketDTO> marketDTOS = marketRequestService.selectMarketRequestList(state);
            if (state == 0 || state == 1) {
                for (MarketDTO marketDTO : marketDTOS) {
                    List<MarketDTO> marketDetailList = marketRequestService.selectMarketDetailByUuid(marketDTO.getUuid());
                    marketDTO.setOrderDetailList(marketDetailList);
                }
                data.setRows(marketDTOS);
            }
            if (state == 2) {
                for (MarketDTO marketDTO : marketDTOS) {
                    List<MarketDTO> checkDTOList = marketRequestService.selectCheckDetailByUuid(marketDTO.getUuid());
                    String endername=marketRequestService.selectEndername(marketDTO.getGoodsuuid());
                    marketDTO.setEndername(endername);
                    marketDTO.setEndtime(new Date());
                    marketDTO.setOrderDetailList(checkDTOList);
                }
                data.setRows(marketDTOS);
            }
        }

        data.setTotal(marketPage.getTotal());
        return Result.ok(data);
    }

    @GetMapping("/listByPage1")
    public Result marketAdd() {
        List<MarketDTO> marketAddDTOList = marketAddService.marketAdd();
        return Result.okRows(marketAddDTOList);
    }

    @GetMapping("/showsupplier")
    public Result marketAdd(@RequestParam("type") Integer type) {
        List<SupplierDTO> supplierDTOList = supplierService.marketSupplier(type);
        return Result.okRows(supplierDTOList);
    }

    @PostMapping("/add")
    public Result addMarket(@RequestBody OrderDetailDTO orderDetails, HttpServletRequest request, HttpServletResponse response) {
        Date date = new Date();
        Integer supplieruuid = orderDetails.getSupplieruuid();
        for (MarketDTO marketDTO : orderDetails.getOrderDetails()) {
            if (marketDTO.getNum()==null){
                return Result.error(400, "添加错误");
            }
            marketDTO.setCreatername((String) request.getSession().getAttribute("name"));
            marketDTO.setCreatetime(date);
            marketDTO.setSupplieruuid(supplieruuid);
            marketDTO.setStoreuuid(sourcingGoodsService.selectStoreId(marketDTO.getGoodsuuid()));
        }

        Integer i = sourcingGoodsService.addMarket(orderDetails.getOrderDetails());

        if (i == 0) {
            return Result.error(400, "添加错误");
        }
        return Result.ok();

    }

    @PostMapping("/doCheck")
    public Result doCheck(@RequestParam("id") Integer uuid, HttpServletRequest request) {
        String checkername = (String) request.getSession().getAttribute("name");
        checkService.doCheckerMarket(uuid, checkername);
        return Result.ok();
    }

    @PostMapping("/doStart")
    public Result doStart(@RequestParam("id") Integer uuid, HttpServletRequest request) {
        String startername = (String) request.getSession().getAttribute("name");
        startService.doStartMarket(uuid, startername);
        //库存oper
        StoreOperDTO storeOpDTO = startService.selectStoreOp(uuid);
        startService.insertStoreOp(storeOpDTO);
        MarketDTO marketDTO = startService.selectGooduuid(uuid);
        startService.updateStoreNum(marketDTO);
        return Result.ok();
    }

}
